package whf.framework.meta;

import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import whf.framework.meta.entity.IProperty;
import whf.framework.util.StringUtils;


/**
 * 业务对象元数据
 * @author wanghaifeng
 *
 */
public class Meta {
	
	private long id;

	private String name;

	private String remarks;

	private Class modelClass;
	
	private Class actionClass;
	
	private Class serviceClass;
	
	private String serviceName;
	
	private Class daoClass;
	
	private String daoName;
	
	private String persistenceTable;
	
	private Class parentModelClass;
	
	private int pageSize = 15;
	
	private String validationName;
	
	private String branch;

	private Set<IProperty> properties;
	
	public Meta() {
	}
	
	public Meta(int id, String name, Class modelClass) {
		this.id = id;
		this.name = name;
		this.modelClass = modelClass;
	}
	
	public Meta(int id, String name,String description, String serviceName, String daoName,String validationName,String persistenceTable,
			Class modelClass,Class actionClass,Class serviceClass,Class daoClass,Class parentModelClass, String branch) {
		this.id = id;
		this.name = name;
		this.remarks=description;
		this.serviceName = serviceName;
		this.daoName=daoName;
		this.validationName=validationName;
		this.persistenceTable = persistenceTable;
		this.modelClass = modelClass;
		this.actionClass=actionClass;
		this.serviceClass = serviceClass;
		this.daoClass=daoClass;
		this.parentModelClass = parentModelClass;
		this.branch = branch;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long boid) {
		id = boid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Class getModelClass() {
		return modelClass;
	}

	public void setModelClass(Class classType) {
		this.modelClass = classType;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String description) {
		this.remarks = description;
	}
	
	/**
	 * @return Returns the actionClass.
	 */
	public Class getActionClass() {
		return actionClass;
	}

	/**
	 * @param actionClass The actionClass to set.
	 */
	public void setActionClass(Class actionClass) {
		this.actionClass = actionClass;
	}

	/**
	 * @return Returns the serviceName.
	 */
	public String getServiceName() {
		return serviceName;
	}

	/**
	 * @param serviceName The serviceName to set.
	 */
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	/**
	 * @return Returns the daoInterface.
	 */
	public Class getDaoClass() {
		return daoClass;
	}

	/**
	 * @param daoInterface The daoInterface to set.
	 */
	public void setDaoClass(Class daoInterface) {
		this.daoClass = daoInterface;
	}

	/**
	 * @return Returns the serviceClass.
	 */
	public Class getServiceClass() {
		return serviceClass;
	}

	/**
	 * @param serviceClass The serviceClass to set.
	 */
	public void setServiceClass(Class serviceClass) {
		this.serviceClass = serviceClass;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int rowsPerPage) {
		this.pageSize = rowsPerPage;
	}

	public String getDaoName() {
		return daoName;
	}

	public void setDaoName(String daoName) {
		this.daoName = daoName;
	}

	public String getPersistenceTable() {
		return persistenceTable;
	}

	public void setPersistenceTable(String persistenceTable) {
		this.persistenceTable = persistenceTable;
	}

	public Class getParentModelClass() {
		return parentModelClass;
	}

	public void setParentModelClass(Class parentModelClass) {
		this.parentModelClass = parentModelClass;
	}

//	public boolean isAuditable() {
//		Class[] interfaces=modelClass.getInterfaces();
//		for(int i=0;interfaces!=null && i<interfaces.length; i++){
//			if(interfaces[i] == Auditable.class){
//				return true;
//			}
//		}
//		return false;
//	}

	public String getValidationName() {
		return validationName;
	}

	public void setValidationName(String validationName) {
		this.validationName = validationName;
	}

	public String getFullClassName(){
		return this.modelClass.getPackage().getName();
	}
	
	public boolean equals(Object obj){
		if(obj == null) return false;
		Meta meta=(Meta)obj;
		return this.id==meta.getId();
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}
	
	public void validate() throws MetaValidationException{
		if(this.modelClass == null){
			throw new MetaValidationException("ModelClass is null!");
		}
		if(this.daoClass == null){
			throw new MetaValidationException("DaoClass is null!");
		}
		if(this.serviceClass == null){
			throw new MetaValidationException("ServiceClass is null!");
		}
		if(StringUtils.isEmpty(this.serviceName)){
			throw new MetaValidationException("Service Name is empty!");
		}
	}

	public Set<IProperty> getProperties() {
		return properties;
	}

	public void setProperties(Set<IProperty> properties) {
		this.properties = properties;
	}
	
	public IProperty findProperty(String propertyName){
		for(IProperty prop: this.properties){
			if(StringUtils.equals(prop.getName(), propertyName)){
				return prop;
			}
		}
		return null;
	}
	
	public static void main(String args[]){
		Class cls=Meta.class;
		System.out.println(cls.getPackage().getName());
		System.out.println(cls.getName());
	}

	public String getBranch() {
		return branch;
	}

	public void setBranch(String branch) {
		this.branch = branch;
	}
}
